package com.sec.bnjey.activity;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.sec.bnjey.BaseActivity;
import com.sec.bnjey.R;
import com.sec.bnjey.bluetooth.BtService;
import com.sec.bnjey.bluetooth.BtUtil;
import com.sec.bnjey.bluetooth.OBDListener;
import com.sec.bnjey.utils.RbLog;

public class RealMileageActivity extends BaseActivity implements OBDListener {
	// get speed cool
	byte[] mRealTimeSpeed = new byte[2];
	// get something.
	byte[] mRealTimeCool = new byte[2];
	// get something.
	byte[] mRealTimeVolt = new byte[2];

	int data0 = 0;
	int data1 = 0;
	int data2 = 0;
	byte data3 = 0x01;
	float data4 = 0;
	byte data5 = 0x01;
	float data6 = 0;
	byte data7 = 0x01;
	float data8 = 0;

	int second = 0;
	int minute = 0;
	int cigan = 0;
	int day = 0;
	int month = 0;
	int mValue = 0;

	private int mRealtimeDriveDistance;
	private int mRealtimeDriveDistanceCheckCnt;

	private boolean mRealtimeDriveIsValid = false;

	private Calendar cal2 = Calendar.getInstance();

	private GetRealTimeDataThread mGetRealTimeDataThread = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.realmileageactivity);

		// RealTimeDataThread 시작
		mGetRealTimeDataThread = new GetRealTimeDataThread();
		mGetRealTimeDataThread.start();
	}

	class GetRealTimeDataThread extends Thread {
		boolean cancel = false;

		public void cancel() {
			cancel = true;
		}

		public void run() {
			while (cancel == false) {

				RbLog.d("RealMileageActivity", "GetRealTimeDataThread" + cancel);

				BtService.getBtService().OBD_Request(BtService.OBD_GET_REAL_DRIVER, null, RealMileageActivity.this);

				// get speed cool
				byte[] reqData = { 0x0D };

				try {
					mRealTimeSpeed = BtService.getBtService().OBD_requestGenericServiceData2(reqData);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				// get something.
				byte[] reqData1 = { 0x05 };

				try {
					mRealTimeCool = BtService.getBtService().OBD_requestGenericServiceData2(reqData1);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				// get something.
				mRealTimeVolt = BtService.getBtService().OBD_VOLTAGE();
				RbLog.d("RealMileageActivity", "Realtimelog s:" + mRealTimeSpeed + " c:" + mRealTimeCool + " v:" + mRealTimeVolt);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void realTimeDataParse(byte[] REAL) {

		if (REAL == null)
			return;

		RbLog.d("RealMileageActivity", "realTimeDataParse");

		byte[] dummy = { 0, 0, 0, 0 };
		dummy[0] = REAL[0];
		dummy[1] = REAL[1];
		dummy[2] = REAL[2];
		dummy[3] = REAL[3];
		data0 = BtUtil.Bytes2Int(dummy);

		dummy[0] = REAL[4];
		dummy[1] = REAL[5];
		dummy[2] = REAL[6];
		dummy[3] = REAL[7];
		data1 = BtUtil.Bytes2Int(dummy);

		dummy[0] = REAL[8];
		dummy[1] = REAL[9];
		dummy[2] = REAL[10];
		dummy[3] = REAL[11];
		data2 = BtUtil.Bytes2Int(dummy);

		if (mRealtimeDriveDistanceCheckCnt < 20) {
			mRealtimeDriveDistanceCheckCnt++;
			mRealtimeDriveDistance += data2;

			RbLog.e("RealMileageActivity", " mRealtimeDriveDistanceCheckCnt : " + mRealtimeDriveDistanceCheckCnt);
			// check
			int sumIndex = 0;
			for (int cntIndex = 0; cntIndex < 20; cntIndex++) {

				sumIndex += (int) REAL[cntIndex];

			}

			RbLog.d("RealMileageActivity", "--- sumIndex --- : " + sumIndex);
			if (sumIndex == 0) {
				mRealtimeDriveIsValid = false;
			} else {
				mRealtimeDriveIsValid = true;
			}
		} else {
			if (mRealtimeDriveIsValid == false) {
				RbLog.e("RealMileageActivity", " = = = = =mRealtimeDriveIsValid == false = = = = =");
				// viewSelectTab(TAB_RUN, true);
				return;
			}
		}

		second = data1 % 60;
		minute = (data1 / 60) % 60;
		cigan = (data1 / 3600) % 24;
		day = (data1 / 86400) % 30;
		month = (data1 / 2592000) % 12;

	}

	private void viewUpdateRealTimeData() {
		RbLog.d("RealMileageActivity", "viewUpdateRealTimeData");

		cal2.set(2012, 0, 1, 0, 0, 0);
		cal2.add(Calendar.SECOND, data1);
		SimpleDateFormat dFormat = new SimpleDateFormat("mm:ss");

		TextView starttime = (TextView) findViewById(R.id.text_mileage1);

		if (cal2.get(Calendar.HOUR_OF_DAY) < 10) {
			starttime.setText("0" + cal2.get(Calendar.HOUR_OF_DAY) + ":" + dFormat.format(cal2.getTime()));
		} else {
			starttime.setText("" + cal2.get(Calendar.HOUR_OF_DAY) + ":" + dFormat.format(cal2.getTime()));
		}

		TextView realtime = (TextView) findViewById(R.id.text_mileage2);
		Calendar cal = Calendar.getInstance();
		cal.set(2012, 0, 1, 0, 0, 0);
		cal.add(Calendar.SECOND, data0 / 1000);
		dFormat = new SimpleDateFormat("mm:ss");

		if (cal.get(Calendar.HOUR_OF_DAY) < 10) {
			realtime.setText("0" + cal.get(Calendar.HOUR_OF_DAY) + ":" + dFormat.format(cal.getTime()));
		} else {
			realtime.setText("" + cal.get(Calendar.HOUR_OF_DAY) + ":" + dFormat.format(cal.getTime()));
		}
		RbLog.d("RealMileageActivity", "real speed : " + data0);

		TextView distance = (TextView) findViewById(R.id.text_mileage3);
		String strAppVersion = String.format("%.1f", (float) data2 / 10);
		distance.setText("" + strAppVersion + " Km");
		TextView speed = (TextView) findViewById(R.id.text_mileage7);
		int i2 = 0;
		if (mRealTimeSpeed != null) {
			if (mRealTimeSpeed[0] == 0x00) {
				speed.setText(getString(R.string.unsupported));
			} else {
				byte[] ag = new byte[1];
				ag[0] = mRealTimeSpeed[1];
				i2 = BtUtil.Bytes2Int(ag);
				speed.setText("" + i2 + " Km/h");
			}
		}

		if (mRealTimeCool != null) {
			if (mRealTimeCool[0] == 0x00) {
				TextView coolant = (TextView) findViewById(R.id.text_mileage8);
				coolant.setText(getString(R.string.unsupported));
			} else {
				byte[] ag = new byte[1];
				ag[0] = mRealTimeCool[1];
				int i = BtUtil.Bytes2Int(ag);
				TextView coolant = (TextView) findViewById(R.id.text_mileage8);
				coolant.setText("" + (i - 40) + "℃");
			}
		}

		if (mRealTimeVolt != null) {

			RbLog.d("RealMileageActivity", "mRealTimeVolt : " + mRealTimeVolt);

			int b1 = BtUtil.Bytes2Int(mRealTimeVolt);
			TextView battery = (TextView) findViewById(R.id.text_mileage9);
			strAppVersion = String.format("%.2f", (float) b1 * 0.004384);
			battery.setText("" + strAppVersion + "V");
		}
	};

	@Override
	public void onOBDEvent(int type, Object retObject) {
		Log.e("RealMileageActivity", "onOBDEvent " + type);

		byte[] data = (byte[]) retObject;
		if (data == null) {
			Log.e("RealMileageActivity", "onOBDEvent ret data is Null, Type:" + type);
		}

		switch (type) {
		case BtService.OBD_GET_REAL_DRIVER: {
			realTimeDataParse(data);
			viewUpdateRealTimeData();
			break;
		}

		default:
			break;
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// 화면 종료시 realTimeThread 종료
		if (mGetRealTimeDataThread != null) {
			mGetRealTimeDataThread.cancel();
		}
	}

}
